package fr.orleans.tools;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;

public class JSFServices {
	
	@SuppressWarnings({"unchecked", "rawtypes", "el-syntax"})
	public static Object getManagedBean(Class oneClass, String name) {
		FacesContext ctx = FacesContext.getCurrentInstance();
		Application app = ctx.getApplication();
		return app.evaluateExpressionGet(ctx, "#{" + name + "}", oneClass);
	}
	
	public static String getHttpParamByName(String paramName) {
		FacesContext context = FacesContext.getCurrentInstance();
		return (String) context.getExternalContext().getRequestParameterMap().get(paramName);
	}
	
	public static Object getParameterInRequestMap (String parameter, FacesContext facesContext){
		return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(parameter);
	}
	
	public static Object getParameterInRequestMap (String parameter) {
		return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(parameter);
	}
	
	public static void putParameterInRequestMap (String parameter, Object value) {
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(parameter, value);
	}
	
	public static void putParameterInRequestMap (String parameter, Object value, FacesContext facesContext){
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(parameter, value);
	}
	
	public static String randomFileName(){
		Random random = new Random();
		Date currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
		String name = String.valueOf(currentTimestamp.getTime())+ "-" + random.nextInt(100);
		return name;
	}
}